package com.yipinhouse.employee.app.appImpl;

import cn.hutool.core.bean.BeanUtil;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.employee.client.BrokerInfoManageClient;
import com.yipinhouse.employee.client.param.AddBrokerParam;
import com.yipinhouse.employee.client.param.OrderHouseParam;
import com.yipinhouse.employee.client.result.ShopBrokerInfoResult;
import com.yipinhouse.employee.service.BrokerInfoService;
import com.yipinhouse.employee.service.dto.BrokerInfoDto;
import com.yipinhouse.employee.service.dto.ShopBrokerInfoDto;
import com.yipinhouse.employee.service.param.AddBrokerServiceParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class BrokerInfoManageApiImpl implements BrokerInfoManageClient {

    @Resource
    private BrokerInfoService brokerInfoService;

    /**
     * 添加员工
     *
     * @param param
     * @return
     */
    @Override
    public Result addBroker(AddBrokerParam param) {
        log.info("添加员工：{}", param);
        AddBrokerServiceParam addBrokerServiceParam = BeanUtil.copyProperties(param, AddBrokerServiceParam.class);
        BrokerInfoDto brokerInfoDto = brokerInfoService.addBroker(addBrokerServiceParam);
        return Result.getInstance(BrokerInfoDto.class).setData(brokerInfoDto);
    }

    @Override
    public Result orderHouse(OrderHouseParam param) {
        log.info("店长锁定房源：参数：{}", param);
        brokerInfoService.orderHouse(param.getHouseId(), param.getHouseType(), param.getBrokerId());
        return Result.getInstance().setMsg("店长锁定房源成功！");
    }

    /**
     * 关联员工与门店
     *
     * @param shopId
     * @param brokerId
     * @return
     */
    @Override
    public Result correlation(Integer shopId, Integer brokerId) {
        if (brokerInfoService.correlation(shopId, brokerId)) {
            return Result.getInstance(boolean.class).setData(true);
        }
        return Result.getInstance(boolean.class).setData(false);
    }

    /**
     * 查询门店员工
     *
     * @param shopId
     * @return
     */
    @Override
    public Result<List<ShopBrokerInfoResult>> getBrokerByShopId(Integer shopId) {
        log.info("查询门店员工入参:{}", shopId);
        List<ShopBrokerInfoDto> shopBrokers = brokerInfoService.getBrokerByShopId(shopId);
        List<ShopBrokerInfoResult> shopBroker = BeanUtil.copyToList(shopBrokers, ShopBrokerInfoResult.class);
        return Result.getInstanceList(ShopBrokerInfoResult.class).setMsg("查询成功").setCode(200).setData(shopBroker);
    }
}
